package com.tingbu.controller;

import com.tingbu.dubbo.service0.service.api.DubboService;
import com.tingbu.dubbo.service2.service.api.DubboService2;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;

/**
 * @Author xiams
 * @Date: 2021/5/7 15:01
 */

@RestController
@RequestMapping("/consumer")
@Slf4j
public class ConsumerController {

    @Value("${service.ip}")
    private String ip;

    @GetMapping("/service")
    public String getConsumer() {
        RestTemplate template = new RestTemplate();
        String result = template.getForObject("http://" + ip + ":50000/provider/service", String.class);
        return "invoke service：" + result;
    }

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    /**
     * @Reference(version = "2.0.0", group = "dubbo",retries = 0,check = false)
     */

    @Reference
    private DubboService2 dubboService2;
    @Reference
    private DubboService dubboService;

    @GetMapping("/restful/service")
    public String getRestfulConsumer() {
        // 去注册中心拿到一个叫“provider”的服务
        ServiceInstance serviceInstance = loadBalancerClient.choose("provider");
        // 通过负载均衡算法拿到服务中的一个实例,uri为http://开头的ip和端口号
        URI uri = serviceInstance.getUri();
        RestTemplate template = new RestTemplate();
        String result = template.getForObject(uri + "/provider/service", String.class);
        return "invoke service：" + result;
    }

    @GetMapping("/dubbo/service2")
    public String getDubboService2() {
        String result = dubboService2.testDubboService2();
        System.out.println("开始发起dubbo的远程调用service2子服务...");
        return "invoke service2：" + result;
    }

    @GetMapping("/dubbo/service0")
    public String getDubboService0() {
        String result = dubboService.invokeDubboService();
        System.out.println("开始发起dubbo的远程调用service0子服务...");
        return "invoke service0：|" + result;
    }

    @Value("${common.name}")
    private String config;

    /**
     * @Value注解无法动态的从客户端配置中动态拿取到配置信息通过Spring提供的ConfigurableApplicationContext
     */

    @Autowired
    private ConfigurableApplicationContext applicationContext;

    /**
     * 初测配置中心配置信息调用
     *
     * @return
     */
    @GetMapping("/nacos/config")
    public String getConfig() {
        log.info("开始调用配置中心测试服务,config:【{}】", config);
        return config;
    }

    /**
     * 初测配置中心配置信息调用
     *
     * @return
     */
    @GetMapping("/nacos/config2")
    public String getConfigActive() {
        String property = applicationContext.getEnvironment().getProperty("common.name");
        log.info("开始调用配置中心测试服务,config:【{}】", property);
        return property;
    }

    /**
     * 拓展配置文件
     *
     * @return
     */
    @GetMapping("/nacos/config4")
    public String getConfig2Active() {
        String name = applicationContext.getEnvironment().getProperty("common.name");
        String addr = applicationContext.getEnvironment().getProperty("common.addr");
        log.info("开始调用配置中心测试服务,config:【{}】,addr:【{}】", name, addr);
        return name + addr;
    }

}
